/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"E24B42D4-AB9E-41E4-A60B-B5D166AD5DEC"}
 */
var fPass2 = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"3F1FDEB4-9CED-4077-A76D-CC67AC3C7181"}
 */
var fPass = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"31653669-4FF3-473A-A9EB-20C742CC5C29"}
 */
function hash(event) {
	hash_password = globals.hashPass(fPass)
	fPass = null
}

/**
 * Callback method when form is (re)loaded.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3D4D8199-8986-4F08-A9BC-D2DCEDC383EB"}
 */
function onLoad(event) {
	elements.lbl_logo.imageURL = url_logo
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A7FCD684-CFCB-43C6-81F9-042EFC5D6F7E"}
 */
function guardar(event) {
	if (fPass)
	{
		if (fPass == fPass2)
		{
			hash_password = globals.hashPass(fPass)
		}
		else
		{
			globals.DIALOGS.showWarningDialog('Aviso','Las contraseñas no coinciden','OK')
			return
		}
	}
	databaseManager.saveData()
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"182546D6-40D4-4E91-AA8E-C5533738861C"}
 */
function onShow(firstShow, event) {
	databaseManager.setAutoSave(false)
	elements.lbl_logo.imageURL = url_logo
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"5F48FC54-CCA8-4B82-A634-62119CD93480"}
 */
function onHide(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6C72B54C-2C42-467F-8653-9703128EDC2F"}
 */
function buscarLogo(event) {
	plugins.file.showFileOpenDialog(null, null, false, new Array("Images", "jpg", "gif", "png", "*"), grabarLogo, 'Seleccione el logo')
}

/**
 * 
 * @param {Array<plugins.file.JSFile>} foto
 * 
 * @properties={typeid:24,uuid:"37352EB4-D540-45F5-A6D5-0D128ECBC41D"}
 */
function grabarLogo(foto){
	var $arch 		= foto[0]
	var $nombre 	= $arch.getName()
	var $extension 	= $nombre.substring($nombre.length - 3,$nombre.length)
	
	if ($arch.getContentType().indexOf('image') == -1)
	{
		globals.DIALOGS.showWarningDialog('Aviso','El archivo debe ser una imagen','OK')
		return
	}
	
	var $img = plugins.images.getImage($arch.getBytes())
	if ($img.getHeight() > 128 || $img.getWidth() > 128)
		$img = $img.resize(128,128)
	
	var $data 	= $img.getData()//$arch.getBytes();
	var $exito
	if (application.getApplicationType() == APPLICATION_TYPES.WEB_CLIENT)
	{
		var $dir 	= plugins.file.getDefaultUploadLocation()+ '/uservs/'+ userv_id
		var $folder = plugins.file.createFile($dir)
		if (!plugins.file.readFile($folder))
			plugins.file.createFolder($dir)
	
		var $file = plugins.file.createFile($dir+'/logo.'+$extension)
		
		if (plugins.file.writeFile($file, $data, $arch.getContentType()))
		{
			$exito = 1
		}
		else
			$exito = 0
	}
	else
	{
		try {
			var $dir = '/uservs/'+ userv_id
			var tFile = plugins.file.createTempFile("logo", "."+$extension)
			plugins.file.writeFile(tFile, $data)
			plugins.file.streamFilesToServer(tFile, $dir+'/logo.'+$extension);
			$exito = 1
			globals.DIALOGS.showInfoDialog('Aviso','La imagen fue subida exitosamente','OK')
		}
		catch(e)
		{
			$exito = 0
		}
	}
	
	if ($exito)
	{
		url_logo = application.getServerURL()+'/uploads/uservs/'+ userv_id+ '/logo.' + $extension
		elements.lbl_logo.imageURL = url_logo
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','No se pudo subir la imagen','OK')
}